﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.OleDb;
using CSM.Controllers;
using CSM.Models;
using System.Text;

namespace CSM.Views.Wenjuan
{
    public partial class WenjuanStatics : System.Web.UI.Page
    {
        public string alert;
        CustomDataContext db = new CustomDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMyData(0);
                int Totals = TotalPageAmount();
                int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
                Label_Total.Text = "共" + pages.ToString();
                Label_CurrenPageAndTo.Text = "页，当前第1" + " 页，转到";
            }
        }
        #region 绑定数据及翻页
        private void BindMyData(int PageIndex)
        {
            int skipAccount = PageIndex * 10;
            //string[] statid=(from a in db.TCrm_WJ_CustomerAnswer select a.StatID).Distinct().ToArray();
            var MyQuery = (from a in db.TCrm_WJ_CustomerAnswer
                           group a by a.StatID into g
                           select new
                           {
                               StatID=g.Key,
                               statName=(from b in db.TCrm_WJ_Statistics where b.StatID==g.Key select b.StatName).Single().ToString(),
                               Num = (from c in db.TCrm_WJ_CustomerAnswer where c.StatID == g.Key select c.CustomerID).Distinct().Count(),
                           }
                           ).Skip(skipAccount).Take(10);
            GridView_Statics.DataSource = MyQuery;
            GridView_Statics.DataBind();
        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {

            int MyQuery = (from a in db.TCrm_WJ_CustomerAnswer select a.StatID).Distinct().Count();
            return MyQuery;
        }
        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {
            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();
            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx);

        }

        #endregion
        protected void Button_Statics_Command(object sender, CommandEventArgs e)
        {
            string statid = e.CommandArgument.ToString();
            Response.Redirect("StaticsDetail.aspx?id="+statid);
        }
    }
}
